package com.clstu.list;

public class LinkedList01 {
    public static void main(String[] args) {
        Node tom = new Node("tom");
        Node chen = new Node("chen");
        Node aLong= new Node("long");
        tom.next=chen;
        chen.next=aLong;
        aLong.pre=chen;
        chen.pre=tom;
        Node first=tom;
        Node last=aLong;
        //从前到后遍历
       printList(first);

        //后到前
        System.out.println("后到前");
        while (last!=null){
            System.out.println(last);
            last=last.pre;
        }

        Node smith = new Node("smith");
        smith.next=chen;
        smith.pre=tom;
        tom.next=smith;
        chen.pre=smith;
        System.out.println("插入后");
        printList(first);

    }
    public static void printList(Node first){
        //用方法调用就把原先的first保护起来了，最后没有改变外面的first的指向
        //相当于只是多了一个副本first指针能够指向外面first指向的对象
        while(first!=null){
            System.out.println(first);
            first=first.next;
        }
    }
}
class Node{
    public Node next;
    public Node pre;
    private String name;

    public Node(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return   "name='" + name ;
    }
}